//
//  ExhibitionProfileViewController.m
//  ppkc2016
//
//  Created by 拼凑的断音 on 16/3/28.
//  Copyright © 2016年 拼凑的断音. All rights reserved.
//

#import "ExhibitionProfileViewController.h"
#import "ExhibitionProfileCell.h"
#import "ExhibitionProfileModel.h"
#import "ScrollImageCell.h"
#import "XXTool.h"
#import "ViewBusinessPosterButton.h"
#import <CoreLocation/CoreLocation.h>
#import "XVCRegionalShowViewController.h"

@interface ExhibitionProfileViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic) UITableView *tableview;

@property (nonatomic) ViewBusinessPosterButton *posterButton;

@property (nonatomic) CLLocationManager *locMgr;

@end

@implementation ExhibitionProfileViewController
{
    ExhibitionProfileModel *useData;
    NSMutableArray<ExhibitionProfileShowTitleModel*> *exhibitionProfileShowTitleArray;
}
-(UITableView*)tableview
{
    if (_tableview==nil)
    {
        _tableview = [[UITableView alloc] init];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:_tableview];
    }
    return _tableview;
}

-(ViewBusinessPosterButton*)posterButton
{
    if (_posterButton==nil)
    {
        _posterButton = [[ViewBusinessPosterButton alloc] init];
        [_posterButton addTarget:self Select:@selector(clickRegionalButton) ForEvent:DKButtonEventPressedEnd];
        [_posterButton setPosterButtonText:NSLocalizedString(@"地区展会", nil)];
        [self.view addSubview:_posterButton];
    }
    return _posterButton;
}

-(void)initParameter
{
    
}

-(void)initUI
{
    [self tableview];
    [self.navigationItem setTitle:NSLocalizedString(@"简介", nil)];
    [self.view setBackgroundColor:[XXTool colorWithBackGround]];
    [self.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"更多"] style:UIBarButtonItemStylePlain target:self action:@selector(clickleft)]];
    [self posterButton];
}

-(void)getData
{
    [ExhibitionProfileModel getExhibitionProfileWithCompletion:^(ExhibitionProfileModel *data) {
        if (data!=nil)
        {
            useData = data;
            exhibitionProfileShowTitleArray = [[NSMutableArray alloc] init];
            for (int i=0; i<data.list.count; i++)
            {
                ExhibitionProfileShowTitleModel* temp = [data.list objectAtIndex:i];
                if ([temp.model_type intValue]==2||[temp.model_type intValue]==3)//2.固定文字 3.固定文字+跳转url
                {
                    [exhibitionProfileShowTitleArray addObject:temp];
                }
            }
            [_tableview reloadData];
        }
    }];
}

-(void)refreshLayout:(CGRect)frame
{
    _tableview.frame = CGRectMake(0, 0, frame.size.width
                                  , frame.size.height-46);
    self.posterButton.frame = CGRectMake(17/2
                                     , _tableview.frame.size.height+_tableview.frame.origin.y+3
                                     , frame.size.width-17
                                     , 40);
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self getData];
    [self locMgr];
}

#pragma mark tableviewDatasource
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0)
    {
        return [UIScreen mainScreen].bounds.size.width*420/750;
    }
    else if (indexPath.row==1)
    {
        return 204;
    }
    else
    {
        return [ExhibitionProfileCell getHeightForExhibitionProfileCell:exhibitionProfileShowTitleArray[indexPath.row-2]];
    }
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return exhibitionProfileShowTitleArray.count+2;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0)
    {
        NSString* reuseIdentifier = @"ScrollImageCell";
        ScrollImageCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
        if(cell==nil)
        {
            cell = [[ScrollImageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.clipsToBounds = YES;
        }
        ExhibitionProfileShowTitleModel* temp;
        for (int i=0; i<useData.list.count; i++)
        {
            ExhibitionProfileShowTitleModel* nowTemp = useData.list[i];
            if ([nowTemp.model_type intValue]==1)
            {
                temp = nowTemp;
            }
        }
        [cell setImageArray:temp.photo_arr whichImage:nil];
        return cell;
    }
    else if (indexPath.row==1)
    {
        NSString* reuseIdentifier = @"ExhibitionImgCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
        if(cell==nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:NSLocalizedString(@"展会简介简介", nil)]];
            img.contentMode = UIViewContentModeScaleAspectFit;
            img.frame = CGRectMake(10, 10, [UIScreen mainScreen].bounds.size.width - 20, 204-20);
            [cell addSubview:img];
            
            UIView *line = [[UIView alloc] initWithFrame:CGRectMake(20
                                                                    , 204-0.5
                                                                    , [UIScreen mainScreen].bounds.size.width-40
                                                                    , 0.5)];
            line.backgroundColor = [XXTool colorWithHexString:@"CBCBCB"];
            [cell addSubview:line];
        }
        return cell;
    }
    else
    {
        NSString* reuseIdentifier = @"ExhibitionProfileCell";
        ExhibitionProfileCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
        if(cell==nil)
        {
            cell = [[ExhibitionProfileCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
            cell.clipsToBounds = YES;
        }
        [cell setcellWithExhibitionProfileData:exhibitionProfileShowTitleArray[indexPath.row-2]];
        return cell;
    }
}

#pragma mark tableviewDelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if (indexPath.row>1)
    {
        ExhibitionProfileShowTitleModel *temp = exhibitionProfileShowTitleArray[indexPath.row-2];
        if ([temp.model_type intValue]==3)
        {
            [WebController pushWebControllerWithSourceVC:self
                                           requestString:temp.mode_address mapTitle:@""];
        }
    }
}

-(void)clickRegionalButton
{
//    [WebController pushWebControllerWithSourceVC:self requestString:@"ppkc.visionfix.me/pdf/test.pdf"];
//    return;
    // 地区展会
    [XVCRegionalShowViewController pushRegionalShowVCWithSourceVC:self WithShowType:AreaShow imgUrl:@""];
}

-(void)clickleft
{
   // [self.navigationController.sideViewController showLeftView];
    [self.navigationController popViewControllerAnimated:YES];
}

@end
